<?php
/**
 * Created by PhpStorm.
 * User: Andy
 * Date: 2019/3/15
 * Time: 14:17
 */

namespace App\api\controller;
use Common\controller\Controller;
use Common\helper\XmlHelper;
use Curl;


/**
 * 协议支付－笔笔验证（电商版 H5） 支付类
 * Class FuyouBbyH5Pay
 * @package App\api\controller
 */
class FuyouBbyPay  extends Controller
{
    private static $key32='mht0zrefxyyspytctz9hdj3bcqzx6orw';
    private static $h5PayUrl='http://www-1.fuioupay.com:18670/mobile_pay/h5pay/payAction.pay';  //支付地址
    private static $orderCheckUrlFuyou='http://www-1.fuioupay.com:18670/mobile_pay/findPay/queryOrderId.pay';  //订单查询接口（富友订单号）
    private static $orderCheckUrlMerchant='http://www-1.fuioupay.com:18670/mobile_pay/checkInfo/checkResult.pay';  //订单查询接口，（商户订单）
    private static $orderCloseUrl='http://www-1.fuioupay.com:18670/mobile_pay/mchntOrderOpr/disableOrder.pay';  //订单关闭接口
    private static $binCodeCheckUrl='http://www-1.fuioupay.com:18670/mobile_pay/findPay/cardBinQuery.pay';   //银行卡bin码查询
    private static $bankLimitAmountUrl='http://www-1.fuioupay.com:18670/mobile_pay/amtlimit/lmtQueryAction.pay';  //银行限额查询

    private static $homeUrl='';
    private static $backUrl='';
    private static $errUrl='';

    /**
     * h5支付接口
     */
    public function h5Pay(){
        //设置一些变量
        self::$backUrl=$_REQUEST['BACKURL'];
        self::$homeUrl=$_REQUEST['HOMEURL'];
        self::$errUrl=$_REQUEST['REURL'];

        //顺序相关，不可乱序
        $dataSign=[
            $_REQUEST['TYPE'],
            $_REQUEST['VERSION'],
            $_REQUEST['MCHNTCD'],
            $_REQUEST['MCHNTORDERID'],
            $_REQUEST['USERID'],
            $_REQUEST['AMT'],
            $_REQUEST['BANKCARD'],
            $_REQUEST['BACKURL'],
            $_REQUEST['NAME'],
            $_REQUEST['IDNO'],
            $_REQUEST['IDTYPE'],
            $_REQUEST['LOGOTP'],
            $_REQUEST['HOMEURL'],
            $_REQUEST['REURL'],
            self::$key32,
        ];
        $sign=md5(implode('|',$dataSign));

        //订单相关数据体
        $fmBody=[
            'MCHNTCD'=>$_REQUEST['MCHNTCD'], //商户代码
            'TYPE'=>$_REQUEST['TYPE'],//交易类型
            'VERSION'=>$_REQUEST['VERSION'],
            'LOGOTP'=>$_REQUEST['LOGOTP'], //1-隐藏，0-显示
            'MCHNTORDERID'=>$_REQUEST['MCHNTORDERID'],//商户订单号
            'USERID'=>$_REQUEST['USERID'], //用户编码
            'AMT'=>$_REQUEST['AMT'], //交易总金额
            'BANKCARD'=>$_REQUEST['BANKCARD'], //银行卡号，非必填
            'BACKURL'=>$_REQUEST['BACKURL'], //后台通知url
            'REURL'=>$_REQUEST['REURL'], //支付失败后跳转的冲洗支付地址
            'HOMEURL'=>$_REQUEST['HOMEURL'], //页面通知地址，支付成功后跳转的地址
            'NAME'=>$_REQUEST['NAME'], //用户姓名，非必填
            'IDTYPE'=>$_REQUEST['IDTYPE'], //证件类型，0-身份证，非必填
            'IDNO'=>$_REQUEST['IDNO'], //身份证号， 非必填
            'REM1'=>$_REQUEST['REM1'],
            'REM2'=>$_REQUEST['REM2'],
            'REM3'=>$_REQUEST['REM3'],
            'SIGNTP'=>'md5',
            'SIGN'=>$sign,
        ];

        //订单 xml数据 构造
        $fmXml='';
        foreach ($fmBody as $k=>$v){
            $fmXml.='<'.$k.'>'.$v.'</'.$k.'>';
        }
        $fmXml="<ORDER>".$fmXml. "</ORDER>";

        $data=[
            'ENCTP'=>$_REQUEST['ENCTP'],
            'VERSION'=>'2.0',
            'MCHNTCD'=>$_REQUEST['MCHNTCD'],
            'FM'=>$fmXml,
        ];


        //curl  post 模拟浏览器
        $curl = new Curl\Curl();
        $curl->setOpt(CURLOPT_HEADER, FALSE);
        $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
        $curl->setOpt(CURLOPT_FOLLOWLOCATION, 1);
        $curl->setOpt(CURLOPT_AUTOREFERER, 1);
        $curl->setOpt(CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        $curl->setOpt(CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);

        $curl->post(self::$h5PayUrl,$data);

        if ($curl->error) {
            echo $curl->error_code;
        }
        else {
            $pattern='/^Location*/';
            $res=preg_grep($pattern,$curl->response_headers);
            header(array_values($res)[0]);
        }
    }

    /**
     * 订单查询接口，富友订单号
     */
    public function checkOrderFuyou(){
        $dataSign=[
            $_REQUEST['MchntCd'],
            $_REQUEST['OrderId'],
            self::$key32,
        ];

        $sign=md5(implode('|',$dataSign));

        $data=[
           'MchntCd'=>$_REQUEST['MchntCd'],//商户代码
           'OrderId'=>$_REQUEST['OrderId'],
           'Sign'=>$sign,
        ];

        //订单 xml数据 构造
        $fmXml='';
        foreach ($data as $k=>$v){
            $fmXml.='<'.$k.'>'.$v.'</'.$k.'>';
        }
        $fmXml="<FM>".$fmXml. "</FM>";
        $data=[
            'FM'=>$fmXml
        ];

        $curl = new Curl\Curl();
        $curl->setOpt(CURLOPT_HEADER, FALSE);
        $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
        $curl->setOpt(CURLOPT_FOLLOWLOCATION, 1);
        $curl->setOpt(CURLOPT_AUTOREFERER, 1);
        $curl->setOpt(CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        $curl->setOpt(CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);

        $curl->post(self::$orderCheckUrlFuyou,$data);

        if ($curl->error) {
            echo $curl->error_code;
        }

        else {
            echo XmlHelper::xml2Json($curl->response);
        }


    }

    /**
     * 订单号查询接口，商户订单查询
     */
    public function checkOrderMerchant(){
        $dataSign=[
            $_REQUEST['VERSION'],
            $_REQUEST['MCHNTCD'],
            $_REQUEST['MCHNTORDERID'],
            self::$key32
        ];

        $sign=md5(implode('|',$dataSign));
        $data=[
            'VERSION'=>$_REQUEST['VERSION'],
            'MCHNTCD'=>$_REQUEST['MCHNTCD'],
            'MCHNTORDERID'=>$_REQUEST['MCHNTORDERID'],
            'SIGN'=>$sign,
            'REM1'=>$_REQUEST['REM1']?:'',//选填
            'REM2'=>$_REQUEST['REM2']?:'',//选填
            'REM3'=>$_REQUEST['REM3']?:'',//选填
        ];

        //订单 xml数据 构造
        $fmXml='';
        foreach ($data as $k=>$v){
            $fmXml.='<'.$k.'>'.$v.'</'.$k.'>';
        }
        $fmXml="<FM><ORDER>".$fmXml. "</ORDER></FM>";
        $data=[
            'FM'=>$fmXml
        ];


        $curl = new Curl\Curl();
        $curl->setOpt(CURLOPT_HEADER, FALSE);
        $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
        $curl->setOpt(CURLOPT_FOLLOWLOCATION, 1);
        $curl->setOpt(CURLOPT_AUTOREFERER, 1);
        $curl->setOpt(CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        $curl->setOpt(CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);

        $curl->post(self::$orderCheckUrlMerchant,$data);

        if ($curl->error) {
            echo $curl->error_code;
        }

        else {
            echo XmlHelper::xml2Json($curl->response);
        }

    }

    /**
     * 关闭未支付订单
     */
    public function closeOrder(){

        $dataSign=[
            $_REQUEST['MCHNTCD'],
            $_REQUEST['MCHNTORDERID']?:'',
            $_REQUEST['ORDERID']?:'',
            $_REQUEST['AMT'],
            self::$key32,
        ];

        $sign=md5(implode('|',$dataSign));

        $data=[
            'MCHNTCD'=>$_REQUEST['MCHNTCD'],//商户代码
            'MCHNTORDERID'=>$_REQUEST['MCHNTORDERID'],
            'ORDERID'=>$_REQUEST['ORDERID'],
            'AMT'=>$_REQUEST['AMT'],
            'SIGN'=>$sign,
        ];

        //订单 xml数据 构造
        $fmXml='';
        foreach ($data as $k=>$v){
            $fmXml.='<'.$k.'>'.$v.'</'.$k.'>';
        }
        $fmXml="<FM>".$fmXml. "</FM>";
        $data=[
            'FM'=>$fmXml
        ];

        $curl = new Curl\Curl();
        $curl->setOpt(CURLOPT_HEADER, FALSE);
        $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
        $curl->setOpt(CURLOPT_FOLLOWLOCATION, 1);
        $curl->setOpt(CURLOPT_AUTOREFERER, 1);
        $curl->setOpt(CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        $curl->setOpt(CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);

        $curl->post(self::$orderCloseUrl,$data);
        if ($curl->error) {
            echo $curl->error_code;
        }

        else {
            echo XmlHelper::xml2Json($curl->response);
        }


    }

    /***
     * 通过bin码查询银行卡信息
     */
    public function checkBankCardInfoByBinCode(){
        $dataSign=[
            $_REQUEST['MchntCd'],
            $_REQUEST['Ono'], //银行卡号
            self::$key32,
        ];

        $sign=md5(implode('|',$dataSign));
        $data=[
            'MchntCd'=>$_REQUEST['MchntCd'],//商户代码
            'Ono'=>$_REQUEST['Ono'],
            'Sign'=>$sign,
        ];

        //订单 xml数据 构造
        $fmXml='';
        foreach ($data as $k=>$v){
            $fmXml.='<'.$k.'>'.$v.'</'.$k.'>';
        }
        $fmXml="<FM>".$fmXml. "</FM>";
        $data=[
            'FM'=>$fmXml
        ];

        $curl = new Curl\Curl();
        $curl->setOpt(CURLOPT_HEADER, FALSE);
        $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
        $curl->setOpt(CURLOPT_FOLLOWLOCATION, 1);
        $curl->setOpt(CURLOPT_AUTOREFERER, 1);
        $curl->setOpt(CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        $curl->setOpt(CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);

        $curl->post(self::$binCodeCheckUrl,$data);
        if ($curl->error) {
            echo $curl->error_code;
        }

        else {
            echo XmlHelper::xml2Json($curl->response);
        }

    }

    /**
     * 银行限额查询
     */
    public function bankLimitAmountCheck(){
        $dataSign=[
            $_REQUEST['MCHNTCD'],
            $_REQUEST['INSCD'], //银行机构号
            self::$key32,
        ];

        $sign=md5(implode('|',$dataSign));
        $data=[
            'MCHNTCD'=>$_REQUEST['MCHNTCD'],//商户代码
            'INSCD'=>$_REQUEST['INSCD'],
            'SIGN'=>$sign,
        ];

        //订单 xml数据 构造
        $fmXml='';
        foreach ($data as $k=>$v){
            $fmXml.='<'.$k.'>'.$v.'</'.$k.'>';
        }
        $fmXml="<FM>".$fmXml. "</FM>";
        $data=[
            'FM'=>$fmXml
        ];

        $curl = new Curl\Curl();
        $curl->setOpt(CURLOPT_HEADER, FALSE);
        $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
        $curl->setOpt(CURLOPT_FOLLOWLOCATION, 1);
        $curl->setOpt(CURLOPT_AUTOREFERER, 1);
        $curl->setOpt(CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        $curl->setOpt(CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);

        $curl->post(self::$bankLimitAmountUrl,$data);
        if ($curl->error) {
            echo $curl->error_code;
        }

        else {
            echo XmlHelper::xml2Json($curl->response);
        }


    }


    /**
     * 支付结果的通知，对结果做处理
     */
    public function notifyBackurl(){
        //支付结果处理

    }

    /**
     * 是支付成功后，在富友的支付成功页面停留几秒后，自动跳转到 homeurl
     * 做支付成功后的数据处理
     */
    public function notifyHomeUrl(){

    }


    /**
     * 是支付失败了，跳转到富友的支付失败页面点击该 URL 跳转到商户页面
     */
    public function notifyErrurl(){

    }

}